package com.itic.system.collage.sku.service;

import com.itic.appbase.framework.common.constants.DBConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.system.collage.sku.persistence.dao.CollageSkuDao;
import com.itic.system.collage.sku.persistence.model.CollageSku;
import com.itic.system.collage.sku.persistence.model.CollageSkuExample;
import com.itic.system.collage.sku.persistence.model.CollageSkuExample.Criteria;


import java.util.List;


@Service
@Transactional
public class CollageSkuService
     extends CrudService<CollageSkuDao,CollageSku,CollageSkuExample> {
    @Autowired
    private CollageSkuDao collageSkuDao;

    /**
     * 添加属性名称
     * @param collageCategoryId 拼团类型Id
     * @param skuName 属性值名称
     * @param collageId 拼团Id
     * @return
     */
    public String addCollageSku(String collageCategoryId,String skuName,String collageId){
        CollageSku collageSku = new CollageSku();
        collageSku.setCollageCategoryId(collageCategoryId);
        collageSku.setSkuName(skuName);
        collageSku.setCollageId(collageId);
        String result = this.add(collageSku);
        return result;
    }

    /**
     * 修改拼团sku属性值名称
     * @param collageSku
     * @param skuName
     * @return
     */
    public String editCollageSku(CollageSku collageSku,String skuName){
        collageSku.setSkuName(skuName);
        String result = this.edit(collageSku);
        return result;
    }

    /**
     * 根据拼团类型Id查询所有商品属性值
     * @param collageCategoryId
     * @return
     */
    public List<CollageSku> findCollageSkuByCategoryId(String collageCategoryId){
        CollageSkuExample example = new CollageSkuExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andCollageCategoryIdEqualTo(collageCategoryId);
        List<CollageSku> collageSkus = this.list(example);
        return collageSkus;
    }

    /**
     * 根据类型删除类型下所有的属性值
     * @param categoryId
     */
    public void delBatchCollageSkuByCategoryId(String categoryId){
        List<CollageSku> collageSkus = this.findCollageSkuByCategoryId(categoryId);
        for(CollageSku collageSku:collageSkus)
            this.delete(collageSku);
    }

    /**
     * 根据拼团Id查询拼团规格
     * @param collageId
     * @return
     */
    public List<CollageSku> findCollageSkuByCollageId(String collageId){
        CollageSkuExample example = new CollageSkuExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andCollageIdEqualTo(collageId);
        List<CollageSku> collageSkus = this.list(example);
        return collageSkus;
    }

}